var showEnergyDetail;
require(['grid', 'topBar', 'layout', 'date','select2'], function () {
	var postData,carId,$top = $('#top');
	//左侧树
	commonFun.leftTreeNodeClickObj['energyDetail'] = carChange;

	function topBarInit() {
		$top.topBar({
			left: [
				{
					text: '选择日期',
					name: 'beginTime',
					type: 'text'
				},
				{
					text: '燃油种类',
					name: 'fuelType',
					width: 100,
					type: 'select',
					data: [
						{value: '柴油', text: '柴油'},
						{value: '汽油', text: '汽油'},
						{value: '电能', text: '电能'},
						{value: 'CNG', text: 'CNG'},
						{value: 'LNG', text: 'LNG'},
						{value: '其他', text: '其他'},
					]
				},
				{
					text: '查询',
					type: 'btn',
					click: function () {
						search();
					}
				}

			],
			right: [
				{
					text: '导出',
					type: 'export-btn',
					authority: '',
					item: [
						{
							text: 'Excel导出',
							click: function () {
								exportFile('Excel');
							}
						},
						{
							text: 'PDF导出',
							click: function () {
								exportFile('Pdf');
							}
						}
					]
				}
			]
		});
	}
	function carChange(event, treeId, treeNode) {
		if (treeNode.type === 'CAR') {
			carId = treeNode.id;
		}
	}

	$('#top').on('focus','[name=beginTime]',function(){
		WdatePicker();
	});

	//获取左侧选择的节点信息
	function getPostData() {
		rcx = commonFun.getTreeCheckNodeToArgs();
		type = commonFun.getLeftTreeCurrentTreeType();
		orgIds = commonFun.getLeftTreeCheckedNodes(type);
		if (rcx) {
			postData = $.extend(rcx, $("#top").parent().getValues());
			postData.type = type;
			postData.orgIds = orgIds;
		}
		return rcx;
	}

	function exportFile(suffix){
		if($('#grid').getGridParam("reccount")>0){
			postData = $("#top").parent().getValues();
			postData.carId = carId;
			postData.endTime =  postData.beginTime;
			$.exportFile("/energyDetail/exportEnergyDetail"+suffix+".htm",postData);
		}
		else{
			$.messager.alert("警告","请先查询数据！","info");
		}
	}

	function gridInit() {
		$('#grid').grid({
			multiselect: false,
			url: "/energyDetail/searchEnergyDetail.htm",
			colModel: [
				{name: "licPltNo", label: "车牌号"},
				{name: "eqmtCode", label: "设备编码", align: "right"},
				{name: "fuelType", label: "燃料类型"},
				{name: "rcrdTime", label: "数据时间"},
				{name: "totalMile", label: "总里程", align: "right"},
				{name: "totalEnerCons", label: "总能耗", align: "right"},
				{name: "totalOprtDura", label: "总时长", align: "right"},
				{name: "rngMile", label: "当日累计里程", align: "right"},
				{name: "rngEnerCons", label: "当日累计能耗", align: "right"},
				{name: "rngOprtDura", label: "当日累计时长", align: "right"}
			]
		});
	}

	function search(){
		if (carId) {
			postData = $("#top").parent().getValues();
			postData.carId = carId;
			postData.endTime =  postData.beginTime;
			$('#grid').grid('setGridParam', {
				page: 1,
				datatype: "json",
				postData: postData
			}).trigger("reloadGrid");
		}else{
			$.messager.alert("提示", "请选择一辆车进行查看。", "info");
		}
	}
	showEnergyDetail = function (data) {
		//切换成车辆
		commonFun.showLeftTree(commonFun.carTree);
		commonFun.setSelectedNodes(data.carId);
		$("input[name='beginTime']").val(data.beginTime);
		//$("#select2-chosen-2").val(data.fuelType)
		//$('#fuelType').select2('val',data.fuelType)
		//$('#select2-chosen-2').select2('val',data.fuelType)
		//$('.topBar').find('[name=fuelType] a').each(function () {
		//	$(this).attr('class', '');
		//	var val = $(this).attr('data-val');
		//	if (val == drvsctbhv) {
		//		$(this).attr('class', 'active');
		//	}
		//});

		$('#grid').grid('setGridParam', {
			page: 1,
			datatype: "json",
			postData: data
		}).trigger("reloadGrid");
	}

	var now = new Date ();
	var year = now.getFullYear ();//获取四位数年数
	var month = now.getMonth () + 1;
	var date = now.getDate ();
	var weeknum = now.getDay ();
	var str = year + "-" + Appendzero (month) + "-" + Appendzero (date);
	function Appendzero (obj) {
		if (obj < 10) {
			return "0" + obj;
		}else{
			return obj;
		}
	}


	//layout 布局
	$('body').layout();
	topBarInit();
	gridInit();
	$("input[name='beginTime']").val(str)
});